{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "42b68886",
   "metadata": {},
   "source": [
    "-----\n",
    "* week 15\n",
    "* 2023-12-11\n",
    "* Flask-web\n",
    "-----"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8dcc6c8",
   "metadata": {},
   "source": [
    "# 1. 创建Flask最小应用\n",
    "> 1. 传递一个文本到指定的路由URL界面\n",
    "> 2. 传递一个HTML的文本信息到指定路由URL界面\n",
    ">> 1. 思考：如果return html的代码，会显得HTML代码很多，且写在了Python文件当中；\n",
    ">> 2. 思考：如果前（HTML）后（PYTHON）端就可以分开两个文件（前后端分离）就比较好了；\n",
    ">> 3. 结论：使用render_template方法实现前后端分离    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6fac3a4f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app '__main__'\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.\n",
      " * Running on http://127.0.0.1:5000\n",
      "Press CTRL+C to quit\n",
      "127.0.0.1 - - [11/Dec/2023 20:40:32] \"GET / HTTP/1.1\" 200 -\n"
     ]
    }
   ],
   "source": [
    "from flask import Flask\n",
    "app = Flask(__name__)\n",
    "\n",
    "@app.route('/')\n",
    "def hello_world():\n",
    "    return 'Hello,World!'\n",
    "\n",
    "@app.route('/index')\n",
    "def index():\n",
    "    return '<h1 >这是我的Python期末项目</h1>'\n",
    "\n",
    "@app.route('/flask')\n",
    "def flask_on():\n",
    "    return '<h1 >这是我的Python期末项目</h1>'\n",
    "\n",
    "app.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35ab996f",
   "metadata": {},
   "source": [
    "# 2. 创建Flask应用，实现静态页面的开发"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "341abd07",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app '__main__'\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.\n",
      " * Running on http://127.0.0.1:5000\n",
      "Press CTRL+C to quit\n",
      "[2023-12-25 14:45:41,654] ERROR in app: Exception on / [GET]\n",
      "Traceback (most recent call last):\n",
      "  File \"D:\\Anaconda3\\Lib\\site-packages\\flask\\app.py\", line 2525, in wsgi_app\n",
      "    response = self.full_dispatch_request()\n",
      "               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"D:\\Anaconda3\\Lib\\site-packages\\flask\\app.py\", line 1822, in full_dispatch_request\n",
      "    rv = self.handle_user_exception(e)\n",
      "         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"D:\\Anaconda3\\Lib\\site-packages\\flask\\app.py\", line 1820, in full_dispatch_request\n",
      "    rv = self.dispatch_request()\n",
      "         ^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"D:\\Anaconda3\\Lib\\site-packages\\flask\\app.py\", line 1796, in dispatch_request\n",
      "    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\10626\\AppData\\Local\\Temp\\ipykernel_5276\\2512982990.py\", line 7, in hello_world\n",
      "    return render_template('index.html')\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"D:\\Anaconda3\\Lib\\site-packages\\flask\\templating.py\", line 146, in render_template\n",
      "    template = app.jinja_env.get_or_select_template(template_name_or_list)\n",
      "               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"D:\\Anaconda3\\Lib\\site-packages\\jinja2\\environment.py\", line 1081, in get_or_select_template\n",
      "    return self.get_template(template_name_or_list, parent, globals)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"D:\\Anaconda3\\Lib\\site-packages\\jinja2\\environment.py\", line 1010, in get_template\n",
      "    return self._load_template(name, globals)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"D:\\Anaconda3\\Lib\\site-packages\\jinja2\\environment.py\", line 969, in _load_template\n",
      "    template = self.loader.load(self, name, self.make_globals(globals))\n",
      "               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"D:\\Anaconda3\\Lib\\site-packages\\jinja2\\loaders.py\", line 126, in load\n",
      "    source, filename, uptodate = self.get_source(environment, name)\n",
      "                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"D:\\Anaconda3\\Lib\\site-packages\\flask\\templating.py\", line 62, in get_source\n",
      "    return self._get_source_fast(environment, template)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"D:\\Anaconda3\\Lib\\site-packages\\flask\\templating.py\", line 98, in _get_source_fast\n",
      "    raise TemplateNotFound(template)\n",
      "jinja2.exceptions.TemplateNotFound: index.html\n",
      "127.0.0.1 - - [25/Dec/2023 14:45:41] \"GET / HTTP/1.1\" 500 -\n"
     ]
    }
   ],
   "source": [
    "from flask import Flask\n",
    "from flask import render_template\n",
    "app = Flask(__name__)\n",
    "\n",
    "@app.route('/')\n",
    "def hello_world():\n",
    "    return render_template('index.html')\n",
    "\n",
    "@app.route('/entry')\n",
    "def entry():\n",
    "    return render_template('entry.html')\n",
    "\n",
    "@app.route('/introduction')\n",
    "def introduction():\n",
    "    return render_template('introduction.html')\n",
    "\n",
    "@app.route('/contact')\n",
    "def contact():\n",
    "    return render_template('contact.html')\n",
    "\n",
    "@app.route('/smart_app')\n",
    "def smart_app():\n",
    "    return render_template('app.html')\n",
    "\n",
    "app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b0cd1b9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12724f79",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
